package com.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //这里runable是线程的一个继承类
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
        //同时根据分析结构，callable是通过FutureTask中转，进而实现的
//        new Thread(new FutureTask<>(Callable));

        MyThread myThread = new MyThread();
        FutureTask futureTask = new FutureTask(myThread);

        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();
        new Thread(futureTask,"C").start();

        Integer o = (Integer) futureTask.get();
        System.out.println(o);

    }
}
//发现Acall()只是输出了一次，而我们明明是开启了两个线程，按理应该是输出两次call()的，这里是因为FutureTask会有缓存，提高执行效率，还有一个我们get()方法时方法会阻塞
//callable 有缓存，会缓存数据

//这里就是典型的，通过callable中转也能实现线程的功能
//class MyThread implements Runnable{
class MyThread implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("call()");
        return 1024;
    }
}
